//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverSetupConquerExec.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_input","_params","_id","_result"];
_input = _this;
_result = false;
if (IS_ARRAY(_input) && {count _input > 2}) then {
	_id = GETVAL(_input,0,"");
	_params = GETVAL(_input,2,[]);
	TRACE_1("Extracted",ARR_2(_id,_params));
	if (IS_ARRAY(_params) && {count _params > 0}) then {
		private ["_object","_parameters","_conquer_zone","_conquer_owner","_shared_variable","_shared_variable_value","_conquer_code","_conquer_zone_center","_conquer_zone_size"
				,"_conquer_zone_angle","_conquer_zone_rect","_object_identifier","_conquer_left","_base_conquer_timeout","_conquer_timeout","_conquer_count"
				,"_conquer_taker","_count_owners","_count_takers","_action_id","_action","_instance_id"];
		_object = GETVAL(_params,0,objNull);
		if (!isNull _object) then {
			_action_id  = GETVAL(_params,1,0);
			_instance_id  = GETVAL(_params,2,0);
			_conquer_shape = GETVAL(_params,3,[]);
			_conquer_owner = GETVAL(_params,4,[]);
			_conquer_taker = GETVAL(_params,5,[]);
			_conquer_left = GETVAL(_params,6,3);
			
			_conquer_zone_center = GETVAL(_conquer_shape,0,getPos _object);
			_conquer_zone_size = GETVAL(_conquer_shape,1,100);
			
			_conquer_count = 0;
			
			TRACE_1("Executing",_params);
			
			_entities = (_conquer_zone_center) nearEntities [["CAManBase","LandVehicle"], _conquer_zone_size];
			
			_count_owners = 0;
			{ _count_owners = _count_owners + (_x countSide _entities);
			} foreach _conquer_owner;
			_count_takers = 0;
			{ _count_takers = _count_takers + (_x countSide _entities);
			} foreach _conquer_taker;
			
			if ((_count_owners <= _conquer_left && _count_takers > 0) || (_conquer_left < 0 && _count_owners < _count_takers)) then {
				_conquer_count = time - (_object getVariable [format["%1_%2",QGVAR(Conquer_Start),_action_id],time]);
			} else {
				_object setVariable [format["%1_%2",QGVAR(Conquer_Start),_action_id],time];
				_conquer_count = 0;
			};
			_object setVariable [format["%1_%2",QGVAR(Conquer_Count),_action_id],_conquer_count];
			TRACE_1("Conquer",ARR_5(_conquer_zone_size, count _entities, _count_owners, _count_takers, _conquer_count));
			
			if (_conquer_count > (_object getVariable [format["%1_%2",QGVAR(Conquer_Timeout),_action_id],15])) then {
				_object setVariable [QGVAR(Conquer_Done),true];
				TRACE_1("Conquered",ARR_2(_count_takers, _conquer_count));
				_action = [_object,_action_id] call FFUNC(actionGet);
				TRACE_1("Action",_action);
				_parameters = [_action] call FFUNC(actionGetParams);
				TRACE_1("Action",_parameters);
				_conquer_msg = [_parameters, "CONQUER_MSG", "",THIS_FILE] call FFUNC(hashGet);
				_conquer_zone = [_parameters, "CONQUER_ZONE", [getPos _object],THIS_FILE] call FFUNC(hashGet);
				_conquer_owner = [_parameters, "CONQUER_OWNER", GVAR(side_enemy),THIS_FILE] call FFUNC(hashGet);
				_shared_variable = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
				_conquer_left = [_parameters, "CONQUER_LEFT", 3,THIS_FILE] call FFUNC(hashGet);
				_conquer_code = [_parameters, "CONQUER_CODE", "",THIS_FILE] call FFUNC(hashGet);
				_conquer_timeout = [_parameters, "CONQUER_TIMEOUT", 15,THIS_FILE] call FFUNC(hashGet);
				_conquer_taker = [_parameters, "CONQUER_TAKER", GVAR(side_own),THIS_FILE] call FFUNC(hashGet);
				_shared_variable = _shared_variable call FFUNC(getStringVar);
	
				if (!isNil "_shared_variable") then {
					if (_shared_variable != "") then {
						[_instance_id,_shared_variable,alive _object] spawn FFUNC(setMissionJIPVar);
						[QGVAR(all_var), [_instance_id,_shared_variable,alive _object],THIS_FILE] call FFUNC(callEvent);
					};
				};
				if (!isNil "_conquer_msg") then {
					if (IS_STRING(_conquer_msg)) then {
						if (_conquer_msg != "") then {
							_conquer_msg = [_conquer_msg ];
						} else {
							_conquer_msg = [];
						};
					};
					if (count _conquer_msg > 0) then {[QGVAR(all_message),[_conquer_msg,"all",objNull,"hq"],THIS_FILE] call FFUNC(callEvent);};
				};
				if (_conquer_code != "") then {
					[QGVAR(all_execute),[_conquer_code,_object,_action],THIS_FILE] call FFUNC(callEvent);
				};
				["itemRemove", _id] call mso_fnc_fsmloop;
			};
		} else {
			DEBUG_1("Object is null",_params);
		};
	} else {
		DEBUG_1("Invalid params",_params);
	};
} else {
	DEBUG_1("Invalid input",_input);
};
TRACE("End");
TMFPROFILERSTOP;